package com.denlaku.longan.config;

import com.denlaku.longan.filter.CommonFilter;
import com.denlaku.longan.filter.LoginFilter;
import com.denlaku.longan.filter.LogoutFilter;
import com.denlaku.longan.filter.RequestContextFilter;
import com.denlaku.longan.filter.SecurityFilter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 基础配置信息
 * @author tianx
 */
@Configuration
@EnableConfigurationProperties(LonganProperties.class)
public class BaseConfig {
    /**
     * 创建请求上下文Filter注册Bean
     *
     * @return Bean
     */
    @Bean
    public FilterRegistrationBean<RequestContextFilter> cusRequestContextFilter() {
        FilterRegistrationBean<RequestContextFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new RequestContextFilter());
        registration.addUrlPatterns("/base/*", "/common/*", "/api/*", "/page/*", "/image/resource/*");
        registration.setName("cusRequestContextFilter");
        registration.setOrder(5);
        return registration;
    }

    /**
     * 创建安全Filter注册Bean
     *
     * @return Bean
     */
    @Bean
    public FilterRegistrationBean<SecurityFilter> cusSecurityFilter() {
        FilterRegistrationBean<SecurityFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new SecurityFilter());
        registration.addUrlPatterns("/base/*", "/common/*", "/api/*", "/page/*", "/image/resource/*");
        registration.setName("cusSecurityFilter");
        registration.setOrder(2);
        return registration;
    }

    /**
     * 创建公共Filter注册Bean
     *
     * @return Bean
     */
    @Bean
    public FilterRegistrationBean<CommonFilter> cusCommonFilter() {
        FilterRegistrationBean<CommonFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/common/*");
        registration.setName("cusCommonFilter");
        registration.setOrder(3);
        return registration;
    }

    /**
     * 创建租户上下文Filter注册Bean
     *
     * @return Bean
     */
    @Bean
    public FilterRegistrationBean<LoginFilter> cusLoginFilter() {
        FilterRegistrationBean<LoginFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/login");
        registration.setName("cusLoginFilter");
        registration.setOrder(0);
        return registration;
    }

    /**
     * 创建租户上下文Filter注册Bean
     *
     * @return Bean
     */
    @Bean
    public FilterRegistrationBean<LogoutFilter> cusLogoutFilter() {
        FilterRegistrationBean<LogoutFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new LogoutFilter());
        registration.addUrlPatterns("/logout");
        registration.setName("cusLogoutFilter");
        registration.setOrder(-1);
        return registration;
    }
}
